package com.mapbased.wfw.components;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import com.mapbased.wfw.*;
import com.mapbased.wfw.PageLoader.StringBufferBag;
import org.springframework.beans.factory.InitializingBean;
import com.mapbased.wfw.PageLoader.LoadContext;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class PageList
    extends Component
{
    Binding pageCount;
    String pageIndex="pageIndex";
    String pageIndexVar="pageIndexVar";

    public static class PageIndexVar
        implements Keep
    {
        int theIndex;
        boolean selected;
        public boolean isSelected()
        {
            return selected;
        }

        public int getPageIndex()
        {
            return theIndex;
        }



    }



    /**
     *
     * @param ele Element
     * @param loader PageLoader
     * @return Element
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public Element parseXml(Element ele,PageLoader.LoadContext lc)
    {
        Attribute item=ele.attribute("pageIndexVar");
        if(item != null)
        {
            this.pageIndexVar=item.getValue().trim();
            ele.remove(item);
        }

        Attribute index=ele.attribute("pageIndex");
        if(index != null)
        {
            this.pageIndex=index.getValue().trim();
            ele.remove(index);
        }

        ///////////////////////////////////////////////////////////////

        Attribute a=ele.attribute("pageCount");
        if(a == null)
        {
            throw new java.lang.RuntimeException(
                "Cannot find data attribute for repeat:" +
                ele.getName());
        }
        String s=a.getValue();
        ele.remove(a);

        LoadContext nlc=new LoadContext(lc);
          List l=nlc.segments;

       lc. loader.parseBinding(s,nlc);
        for(int i=0;i < l.size();i++)
        {
            Object o=l.get(i);
            if(o instanceof Binding)
            {
                this.pageCount=(Binding)o;
                return ele;
            }
        }
        throw new java.lang.RuntimeException("Cannot find binding for repeat:" +
                                             ele.getName());

    }

    /**
     * render
     *
     * @param context RenderContext
     * @throws IOException
     * @todo Implement this com.mapbased.wfw.Component method
     */
    private static int LISTCOUNT=16;
    public void render(RenderContext ctx)
        throws IOException
    {
        Object o=this.pageCount.getValue(ctx);
        int pc=1;
        if(o != null)
        {
            pc=((Number)o).intValue();
        }
        int cp=ctx.getSureSingleInt(this.pageIndex); //currentpage

        if(cp < 1)
        {
            cp=1;
        }
        int up=cp + LISTCOUNT / 2;
        int down=cp - LISTCOUNT / 2;

        if(down < 1)
        {
            up=Math.min(pc,up - down);
            down=1;
        }
        if(up > pc)
        {
            down=Math.max(1,down - up + pc);
            up=pc;
        }
        PageIndexVar var=new PageIndexVar();
        ctx.getRequestMap().put(this.pageIndexVar,var);

        if(down > 1)
        {
            var.selected=cp == 1;
            var.theIndex=1;
            //   ctx.getRequestMap().put(this.pageIndexVar,new Integer(1));
            this.defalutRenderBehavior(ctx);

        }
        for(int i=down;i <= up;i++)
        {
            var.selected=cp == i;

            if(var.selected)
            {
                ctx.getOutputWriter().write("<b>");
            }
            var.theIndex=i;
            this.defalutRenderBehavior(ctx);

            if(var.selected)
            {
                ctx.getOutputWriter().write("</b>");
            }

        }

        if(up < pc)
        {
            var.selected=cp == pc;
            var.theIndex=pc;
            this.defalutRenderBehavior(ctx);

        }

    }

    /**
     * renderHeader
     *
     * @param context RenderContext
     * @param sb StringBuffer
     * @param m Map
     * @todo Implement this com.mapbased.wfw.Component method
     */
    public void renderHeader(RenderContext context,StringBuffer sb,Map m)
    {
    }
}
